from fastapi import APIRouter, Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel
from ...core.security import token_manager, password_manager

router = APIRouter()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")

class User(BaseModel):
    username: str
    password: str

@router.post("/register")
async def register(user: User):
    # 这里应该有更多的验证逻辑
    hashed_password = password_manager.get_password_hash(user.password)
    # 这里应该将用户信息存储到数据库
    return {"message": "User registered successfully"}

@router.post("/token")
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    # 这里应该验证用户凭据
    # 为了演示，我们假设用户名为 "testuser"，密码为 "testpassword"
    if form_data.username != "testuser" or form_data.password != "testpassword":
        raise HTTPException(status_code=400, detail="Incorrect username or password")
    
    access_token = token_manager.create_token({"sub": form_data.username})
    return {"access_token": access_token, "token_type": "bearer"}